import Vue from 'vue'
import VueRouter from 'vue-router'

//  component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
const Login = () => import('../views/login/Login.vue')
const Main = () => import('../views/main/Main.vue')


Vue.use(VueRouter)

const routes = [{
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  }, {
    path: "/main",
    component: Main,
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //如果登录请求  直接放行
  if (to.path === '/login') return next()
  //不是登录请求  查看是否有token 有放行 没有去登录
  const token = window.sessionStorage.getItem('token');
  if (!token) {
    return next('/login')
  }
  next()
})

export default router